﻿@using KimPhongCRM.Models
@{
    List<sp_Select_Combobox_Roles_Result> listRole = ViewData["Roles"] as List<sp_Select_Combobox_Roles_Result>;
    List<sp_Select_Combobox_Permission_Result> listPermission = ViewData["Permission"] as List<sp_Select_Combobox_Permission_Result>;
    var grid = Html.DevExpress().GridView(settings =>
    {
        settings.Name = "listRolePermission";
        settings.CallbackRouteValues = new { Controller = "RolePermission", Action = "listRolePermissionPartial" };

        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "RolePermission", Action = "listRolePermissionPartialAddNew" };
        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "RolePermission", Action = "listRolePermissionPartialUpdate" };
        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "RolePermission", Action = "listRolePermissionPartialDelete" };
        settings.SettingsEditing.Mode = GridViewEditingMode.EditFormAndDisplayRow;
        settings.SettingsBehavior.ConfirmDelete = true;
        settings.CommandColumn.ButtonType = System.Web.UI.WebControls.ButtonType.Image;
        settings.CommandColumn.SetColVisibleIndex(6);
        settings.CommandColumn.SetHeaderCaptionTemplateContent("Tùy chọn");
        settings.CommandColumn.Width = 100;

        settings.CommandColumn.NewButton.Image.Url = @Url.Content("~/Content/img/icon-24-new.png");
        settings.CommandColumn.NewButton.Image.ToolTip = "Thêm mới";
        settings.CommandColumn.NewButton.Image.AlternateText = "Thêm";

        settings.CommandColumn.EditButton.Visible = true;
        settings.CommandColumn.EditButton.Image.Url = @Url.Content("~/Content/img/icon-24-edit.png");
        settings.CommandColumn.EditButton.Image.ToolTip = "Thay đổi";
        settings.CommandColumn.EditButton.Image.AlternateText = "Sửa";

        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.DeleteButton.Image.Url = @Url.Content("~/Content/img/icon-24-delete.png");
        settings.CommandColumn.DeleteButton.Image.ToolTip = "Xóa";
        settings.CommandColumn.DeleteButton.Image.AlternateText = "Xóa";

        settings.CommandColumn.UpdateButton.Visible = true;
        settings.CommandColumn.UpdateButton.Image.Url = @Url.Content("~/Content/img/icon-24-ok.png");
        settings.CommandColumn.UpdateButton.Image.ToolTip = "Lưu lại";
        settings.CommandColumn.UpdateButton.Image.AlternateText = "Lưu";

        settings.CommandColumn.CancelButton.Visible = true;
        settings.CommandColumn.CancelButton.Image.Url = @Url.Content("~/Content/img/icon-24-cancel.png");
        settings.CommandColumn.CancelButton.Image.ToolTip = "Hủy";
        settings.CommandColumn.CancelButton.Image.AlternateText = "Hủy";

        settings.CommandColumn.Visible = true;
        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.EditButton.Visible = true;

        settings.KeyFieldName = "rpid";

        settings.SettingsPager.Visible = true;
        settings.Settings.ShowGroupPanel = true;
        settings.Settings.ShowFilterRow = true;
        settings.SettingsBehavior.AllowSelectByRowClick = true;

        settings.Columns.Add(column =>
        {
            column.FieldName = "RoleId";
            column.Caption = "Nhóm người dùng";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            foreach (var value in listRole)
            {
                comboBoxProperties.Items.Add(value.RoleName, value.RoleId);
            }
            comboBoxProperties.ValueType = typeof(Guid);
            column.ReadOnly = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "permId";
            column.Caption = "Quyền truy cập";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            foreach (var value in listPermission)
            {
                comboBoxProperties.Items.Add(value.permName,value.permId);
            }
            comboBoxProperties.ValueType = typeof(Guid);
            column.ReadOnly = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "isActive";
            column.Caption = "Cho phép";
            column.ColumnType = MVCxGridViewColumnType.CheckBox;
            column.Width = 60;
            column.SetDataItemTemplateContent(c =>
            {
                bool actived = (bool)DataBinder.Eval(c.DataItem, "isActive");
                object rpid_obj = DataBinder.Eval(c.DataItem, "rpid");
                string updateLink = Url.Action("ToogleActive", "Base", new { keyvalue = rpid_obj, active = !actived, tablename = "tblRolePermission", keyname = "rpid" });
                ViewContext.Writer.Write(
                    "<a href=\"" + updateLink + "\" id=\"id_button_active_" + rpid_obj + "\" class=\"button btn-active\"><i class=\"font-icon-" + (actived ? "check" : "check-empty") + "\"></i></a>"
                );
            });
        });
    });
    if (ViewData["EditError"] != null)
    {
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
}
@grid.Bind(Model).GetHtml()